﻿Imports UCBase
Imports System.Drawing
Imports AForge.Imaging.Filters

Public Class StarTracker
    Implements I_LoadSave
    Implements I_ImageExchange
    Private WithEvents SourceEvent As I_ImageExchange
    Public Function GetSettings() As String Implements UCBase.I_LoadSave.GetSettings
        Return ""
    End Function

    Public Sub SetSettings(ByVal Settings As String) Implements UCBase.I_LoadSave.SetSettings

    End Sub

    Public Event ImageOut1(ByVal Image As System.Drawing.Bitmap) Implements UCBase.I_ImageExchange.ImageOut1

    Private Sub SocketIn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SocketIn.Click

    End Sub

    Private Sub SocketIn_Connect() Handles SocketIn.Connect
        Dim S As Socket = SocketIn.Remote
        SourceEvent = S.Parent
    End Sub
    Public Sub Test(ByVal Image As Bitmap)
        SourceEvent_ImageOut1(Image)
    End Sub
    Private Sub SourceEvent_ImageOut1(ByVal Image As System.Drawing.Bitmap) Handles SourceEvent.ImageOut1

        Dim filter As Grayscale = New Grayscale(0.2125, 0.7154, 0.0721)
        Dim Tmp As Bitmap = filter.Apply(Image)
        Dim binF As Threshold = New Threshold(KnobControl1.Value)
        Dim DestBitmap As Bitmap = binF.Apply(Tmp)

        'RaiseEvent ImageOut1(Image)

        Dim BmData As New PixelData(DestBitmap)
        Dim Line(BmData.Width) As Integer
        Dim LineY(BmData.Height) As Integer
        Dim DetX(BmData.Height) As Integer
        Dim DetY(BmData.Width) As Integer
        Dim C As Integer = 0
        For Dy = 0 To BmData.Height - 1
            For Dx = 0 To BmData.Width - 1
                Line(Dx) = BmData.Pixel(Dx, Dy)
            Next
            C = 0
            For dx = 0 To BmData.Width - 1
                If Line(dx) > 125 Then
                    C = C + 1
                End If
                DetX(Dy) = C
            Next
        Next

        For Dx = 0 To BmData.Width - 1
            For Dy = 0 To BmData.Height - 1
                LineY(Dy) = BmData.Pixel(Dx, Dy)
            Next
            C = 0
            For Dy = 0 To BmData.Height - 1
                If LineY(Dy) > 125 Then
                    C = C + 1
                End If
                DetY(Dx) = C
            Next
        Next

        Dim GXBmp As New Bitmap(BmData.Height, DetX.Max + 10, Imaging.PixelFormat.Format24bppRgb)
        Dim Gg As Graphics = Graphics.FromImage(GXBmp)
        Gg.Clear(Color.Black)
        For C = 0 To DetY.Length - 1
            Gg.DrawLine(Pens.Blue, C, 0, C, DetY(C))
        Next
        PictureBox2.Image = GXBmp

        Dim GYBmp As New Bitmap(DetY.Max + 10, BmData.Width, Imaging.PixelFormat.Format24bppRgb)
        Dim Gyg As Graphics = Graphics.FromImage(GYBmp)
        Gyg.Clear(Color.Black)
        For C = 0 To DetX.Length - 1
            Gyg.DrawLine(Pens.Blue, 0, C, DetX(C), C)
        Next
        PictureBox3.Image = GYBmp

        Dim BXa As Integer
        Dim BXb As Integer
        Dim BYa As Integer
        Dim BYb As Integer

        For C = 0 To DetX.Length - 1
            If DetX(C) > 0 And BXa = 0 Then BXa = C
        Next
        For C = DetX.Length - 1 To 0 Step -1
            If DetX(C) > 0 And BXb = 0 Then BXb = C
        Next

        For C = 0 To DetY.Length - 1
            If DetY(C) > 0 And BYa = 0 Then BYa = C
        Next
        For C = DetY.Length - 1 To 0 Step -1
            If DetY(C) > 0 And BYb = 0 Then BYb = C
        Next

        BXa = BXa - 10
        BXb = BXb + 10
        BYa = BYa - 10
        BYb = BYb + 10

        Dim Xc As Integer = Array.IndexOf(DetX, DetX.Max)
        Dim Yc As Integer = Array.IndexOf(DetY, DetY.Max)
        Label4.Text = Xc & " - " & Yc
        Dim bmp As New Bitmap(Tmp)
        Dim G As Graphics = Graphics.FromImage(bmp)
        Dim X As Integer = bmp.Width / 2
        Dim y As Integer = bmp.Height / 2
        Dim GP As New Pen(Color.Green, 5)
        G.DrawLine(GP, X, 0, X, bmp.Height)
        G.DrawLine(GP, 0, y, bmp.Width, y)

        Dim P As New Pen(Color.Red, 5)
        G.DrawLine(P, BYa, BXa, BYa + 30, BXa)
        G.DrawLine(P, BYa, BXb, BYa + 30, BXb)
        G.DrawLine(P, BYb, BXa, BYb - 30, BXa)
        G.DrawLine(P, BYb, BXb, BYb - 30, BXb)

        G.DrawLine(P, BYa, BXa, BYa, BXa + 30)
        G.DrawLine(P, BYb, BXa, BYb, BXa + 30)
        G.DrawLine(P, BYa, BXb, BYa, BXb - 30)
        G.DrawLine(P, BYb, BXb, BYb, BXb - 30)

        PictureBox1.Image = bmp

    End Sub

    Private Sub KnobControl1_ValueChanged(ByVal Sender As System.Object) Handles KnobControl1.ValueChanged

    End Sub
End Class
